package com.xs.project.service.impl;

import com.xs.project.common.Result;
import com.xs.project.email.MailServiceImpl;
import com.xs.project.mapper.ExamManagerMapper;
import com.xs.project.mapper.ExamTitleMapper;
import com.xs.project.mapper.XsCourseMapper;
import com.xs.project.pojo.vo.ExamListReq;
import com.xs.project.pojo.vo.resp.ExamTitleStu;
import com.xs.project.pojo.vo.resp.UserInfoAndCourse;
import com.xs.project.pojo.vo.resp.XsExamResp;
import com.xs.project.service.ExamManagerService;
import com.xs.project.utils.DateUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ExamManagerServiceImpl implements ExamManagerService {
    @Resource
    private ExamManagerMapper examManagerMapper;
    @Resource
    private XsCourseMapper xsCourseMapper;
    @Resource
    private ExamTitleMapper examTitleMapper;
    @Resource
    private MailServiceImpl mailServiceImpl;

    @Override
    public Result<Boolean> insertExamList(XsExamResp request) {
        Integer size = xsCourseMapper.selectById(Integer.valueOf(request.getCourseId()));
        if (size == 0){
            return Result.fail("课程不存在");
        }
        int hasExam = examManagerMapper.selectCourseById(request.getCourseId());
        if (hasExam > 0){
            return Result.fail("该课程已存在考试");
        }
        request.setStatus("0");
        examManagerMapper.insertExamList(request);
        return Result.success(true);
    }

    @Override
    public Result<Boolean> updateExamList(XsExamResp request) {
        if ("1".equals(request.getStatus())){
            Integer i = examTitleMapper.selectById(request.getId());
            if (i == null || i == 0){
                return Result.fail("发布考试,请先添加试题");
            }
        }
        examManagerMapper.updateExamList(request);
        return Result.success(true);
    }

    @Override
    public Result<List<XsExamResp>> selectExamList(ExamListReq request) {
        List<XsExamResp> list = examManagerMapper.selectExamList(request);
        long skipSize = (long) request.getPageSize() * (request.getPageNum() - 1); // 计算需要跳过的元素数量

        List<XsExamResp> pageRes = list.stream()
                .skip(skipSize)
                .limit(request.getPageSize())
                .collect(Collectors.toList());
        for (XsExamResp pageRe : pageRes) {
            pageRe.setExamTimeStr(DateUtil.getDateStr(pageRe.getExamTime()));
            if ("0".equals(pageRe.getStatus())){
                pageRe.setStatusStr("未发布");
            }else {
                pageRe.setStatusStr("已发布");
            }

        }
        return Result.success(pageRes,list.size());
    }

    @Override
    public Result<Boolean> delExamList(XsExamResp request) {
        examManagerMapper.delExamList(request);
        return Result.success(true);
    }

    @Override
    public Result<List<ExamTitleStu>> selectExamTitleStu(String teacherId) {
        List<String> courseIds = examManagerMapper.selectCourseId(teacherId);
        if (CollectionUtils.isEmpty(courseIds)){
            return Result.success(new ArrayList<>());
        }
        List<ExamTitleStu> res = new ArrayList<>();
        for (String courseId : courseIds) {
            List<ExamTitleStu> list = examManagerMapper.selectExamTitleStu(teacherId,courseId);
            res.addAll(list);
        }
        return Result.success(res);
    }

    @Override
    public Result<Boolean> saveExamTitleStu(String id, String point,String userId) {
        examManagerMapper.saveExamTitleStu(id,point);
        int count = examManagerMapper.selectUnHandle(userId);
        if (count == 0){
            Integer totalPoint = examManagerMapper.seletPoint(userId);
            examManagerMapper.updatePoint(totalPoint,userId);
        }
        return Result.success(true);
    }

    @Override
    public Result<Boolean> pushEmail(String examId) {
        XsExamResp resp = examManagerMapper.selectCourseByExamId(examId);
        resp.setExamTimeStr(DateUtil.getDateStr(resp.getExamTime()));
        List<String> emails = examManagerMapper.selectEmails(examId);
        String content = resp.getExamName()+"考试时间:"+resp.getExamTimeStr();
        for (String email : emails) {
            mailServiceImpl.sendSimpleText(email,"考试通知",content);
        }
        return Result.success(true);
    }
}
